package com.liaowei.novel.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.liaowei.novel.R;
import com.liaowei.novel.domain.History;

import java.util.Objects;

public class ReadActivity extends AppCompatActivity implements View.OnClickListener {

    private SQLiteDatabase db;
    private TextView chapter_title, novel_content;
    private Button preChapterBtn, nextChapterBtn;
    private int currentChapter;
    private Cursor cursor;
    int novelId;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        Intent intent = getIntent();
        novelId = intent.getIntExtra("novelId", 0);
        int userId = intent.getIntExtra("userId", 8);
        init();

        // 查询用户历史记录
        History history = getChapterIdByHistory(userId, novelId);

        // 判断有无历史
        int cId = 0;
        String title = "";
        String content = "";
        String sql = "";
        if (history == null) {
            // 没有浏览过该历史，从第一张开始阅读
            sql = "select * from chapter where novel_id = " + novelId + " and number = 1";
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                cId = cursor.getInt(cursor.getColumnIndex("id"));
                title = cursor.getString(cursor.getColumnIndex("title"));
                content = cursor.getString(cursor.getColumnIndex("content"));
            }
            currentChapter = 1;
            // 新增历史记录
            ContentValues cv = new ContentValues();
            cv.put("user_id",8);
            cv.put("novel_id",novelId);
            cv.put("chapter_id",cId);
            cv.put("page_num",1);
            db.insert("history",null,cv);

        } else {
            Integer pageNum = history.getPageNum();
            // 有浏览历史，根据章节id查询对应章节信息
            sql = "select * from chapter where novel_id = " + novelId + " and number = " + pageNum;
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                cId = cursor.getInt(cursor.getColumnIndex("id"));
                title = cursor.getString(cursor.getColumnIndex("title"));
                content = cursor.getString(cursor.getColumnIndex("content"));
            }
            currentChapter = pageNum;
        }
        chapter_title.setText(title);
        novel_content.setText(content);

        // 绑定单击事件
        nextChapterBtn.setOnClickListener(this);
        preChapterBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        String sql;
        int chapterId = 0;
        switch (id) {
            case R.id.nextChapterBtn: // 下一章
                currentChapter ++;
                sql = "select * from chapter where novel_id = " + novelId + " and number =  " + currentChapter;
                cursor = db.rawQuery(sql, null);
                if (cursor.moveToNext()) {
                    chapterId = cursor.getInt(cursor.getColumnIndex("id"));
                    chapter_title.setText(cursor.getString(cursor.getColumnIndex("title")));
                    novel_content.setText(cursor.getString(cursor.getColumnIndex("content")));
                }else {
                    Toast.makeText(this,"已经是最后一章了",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.preChapterBtn: // 上一章
                if (currentChapter == 1){
                    Toast.makeText(this,"已经是第一章了",Toast.LENGTH_LONG).show();
                }else {
                    currentChapter --;
                    sql = "select * from chapter where novel_id = " + novelId + " and number =  " + currentChapter;
                    cursor = db.rawQuery(sql, null);
                    if (cursor.moveToNext()) {
                        chapterId = cursor.getInt(cursor.getColumnIndex("id"));
                        chapter_title.setText(cursor.getString(cursor.getColumnIndex("title")));
                        novel_content.setText(cursor.getString(cursor.getColumnIndex("content")));
                    }
                }
                break;
        }
        // 修改历史记录
        ContentValues cv = new ContentValues();
        cv.put("chapter_id",chapterId);
        cv.put("page_num",currentChapter);
        db.update("history",cv,"user_id = ? and novel_id = ?",new String[]{String.valueOf(8),String.valueOf(novelId)});

    }

    // 根据用户id和小说id查询用户有无历史记录
    public History getChapterIdByHistory(int userId, int novelId) {
        String sql = "select * from history where user_id = " + userId + " and novel_id = " + novelId + " limit 1";
        cursor = db.rawQuery(sql, null);
        History history = null;
        if (cursor.moveToNext()) {
            history = new History();
            Integer id = cursor.getInt(cursor.getColumnIndex("id"));
            Integer chapterId = cursor.getInt(cursor.getColumnIndex("chapter_id"));
            Integer pageNum = cursor.getInt(cursor.getColumnIndex("page_num"));
            history.setId(id);
            history.setChapterId(chapterId);
            history.setPageNum(pageNum);
        }
        return history;
    }

    // 初始化组件
    public void init() {
        this.chapter_title = findViewById(R.id.chapter_title);
        this.novel_content = findViewById(R.id.novel_content);
        this.preChapterBtn = findViewById(R.id.preChapterBtn);
        this.nextChapterBtn = findViewById(R.id.nextChapterBtn);
        this.db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.liaowei.novel/databases/novel.db", null);
    }

}